//
//  BorderDFHViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/5/22.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BorderDFHViewController.h"
#import "BorderAminListmodel.h"
#import "UITableViewFootview.h"
#import "BshopAminTableViewCell.h"
#import "BorderContentViewController.h"

@interface BorderDFHViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *mydateSource;

@property(nonatomic,assign)NSInteger dayind;
@property(nonatomic,strong)UITableViewFootview *footview;

@end

@implementation BorderDFHViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    [self.myTableView reloadData];
    
    // Do any additional setup after loading the view.
    [self request];
    
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refashaction:) name:@"refashA" object:nil];
}
-(void)refashaction:(NSNotification *)noti{
    [self request];
    
    
}
-(void)request{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    kWeakSelf;
    [NetWorkTool POST:BshopshopOrderList param:param success:^(id dic) {
        [self.mydateSource removeAllObjects];
        [self.mydateSource addObjectsFromArray:[BorderAminListmodel mj_objectArrayWithKeyValuesArray:[dic objectForKey:@"data"]]];
        
        NSMutableArray *muarr = [NSMutableArray arrayWithArray:weakSelf.mydateSource];
        
        for (BorderAminListmodel *dicmu  in muarr) {
            if ([dicmu.state integerValue] != 1) {
                [self.mydateSource removeObject:dicmu];
                
            }
            
            
        }
        if ([self.mydateSource count] == 0) {
            ShowToastWithText(@"暂无数据");
        }
        ////    状态(0代付款1待发货2待收货3待评价4已评价
        
        
                NSString *label = [NSString stringWithFormat:@"%lu",(unsigned long)self.mydateSource.count];
                NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:label,@"one", nil];
                NSNotification *notification =[NSNotification notificationWithName:@"tongzhiRefish11" object:nil userInfo:dict];
                //通过通知中心发送通知
                [[NSNotificationCenter defaultCenter] postNotification:notification];
        [self.myTableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.myTableView.mj_header endRefreshing];

    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.myTableView.mj_header endRefreshing];

    } fail:^(NSError *error) {
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.myTableView.mj_header endRefreshing];

        
        
    } needUser:YES];
  
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    BorderAminListmodel *model = _mydateSource[section];
    
    return model.pros.count;
    
}
-
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _mydateSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *heiader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 0.1)];
    
    
    
    return heiader;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    BorderAminListmodel *model = _mydateSource[section];
    
    NSInteger ind = [model.state integerValue];
    if (ind == 1) {
        return 154;
    }else{
        return 114;
        
        
    }
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    BorderAminListmodel *model = _mydateSource[section];
    _footview = [[UITableViewFootview alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 154)];
    
    NSInteger ind = [model.state integerValue];
    if (ind == 1) {
        self.footview.height = 154;
        _footview.myblco = ^(NSString * _Nonnull str) {
            //发货
            [self fahuoaction:model];
        };
    }else{
        self.footview.height = 114;
        [_footview.fahupaction removeFromSuperview];
        
    }
    
    
    if ([model.send_type_id integerValue] == 0) {
        _footview.wuliutypeImage.image = imgname(@"shopziti");
    }else if ([model.send_type_id integerValue] == 1){
        _footview.wuliutypeImage.image = imgname(@"shopwuliu");
        
    }else if ([model.send_type_id integerValue] == 2){
        _footview.wuliutypeImage.image = imgname(@"shopdidipeisong");
        
    }
    _footview.gongjiLB.text = [NSString stringWithFormat:@"共计%.0f件产品",model.count];
    _footview.priceLB.text = [NSString stringWithFormat:@"¥%@",model.count_money];
    
    _footview.addnameLb.text = [NSString stringWithFormat:@"%@",model.user_name];
    _footview.addphoneLB.text = [NSString stringWithFormat:@"%@",model.phone];
    _footview.addcontentLB.text = [NSString stringWithFormat:@"%@",model.address];
    
    
    
    ////    状态(0代付款1待发货2待收货3待评价4已评价
    //    switch (ind) {
    //        case 0:
    //        {
    //            self.stateLb.text =@"待付款";
    //        }
    //            break;
    //        case 1:
    //        {
    //            self.stateLb.text =@"待发货";
    //
    //        }
    //            break;
    //        case 2:
    //        {
    //            self.stateLb.text =@"待收货";
    //
    //        }
    //            break;
    //        case 3:
    //        {
    //            self.stateLb.text =@"待评价";
    //
    //        }
    //            break;
    //        case 4:
    //        {
    //            self.stateLb.text =@"已评价";
    //
    //        }
    //            break;
    //
    //
    //        default:
    //            break;
    //    }
    
    return _footview;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 140;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BshopAminTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BshopAminTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BorderAminListmodel *model = [_mydateSource safeObjectAtIndex:indexPath.section];
    
    ProsModel *poM0del = [model.pros safeObjectAtIndex:indexPath.row];
    poM0del.state = model.state;
    poM0del.create_time1 = model.create_time;
    poM0del.order_number = model.order_number;
    
    [cell refashwithmodel:poM0del];
    kWeakSelf;
    
    
    
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BorderAminListmodel *model = _mydateSource[indexPath.section];
    
    BorderContentViewController *dfesd = [[BorderContentViewController alloc]init];
    dfesd.model = model;
    
    dfesd.mhyblcok = ^(NSInteger ind) {
        [self request];
    };
    [self.navigationController pushViewController:dfesd animated:YES];
    
}
-(void)fahuoaction:(BorderAminListmodel *)model{
    
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"确定现在发货？" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertCon addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        KMyLog(@"取消");
    }]];
    [alertCon addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        KMyLog(@"确定");
        NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
        param[@"id"] = NOTNIL(model.userID);
        
        [NetWorkTool POST:BshopdoSendProt param:param success:^(id dic) {
            ShowToastWithText(@"发货成功");
            [self request];
            
        } other:^(id dic) {
            ShowToastWithText(dic[@"msg"]);
        } fail:^(NSError *error) {
            
        } needUser:YES];
    }]];
    
    [self presentViewController:alertCon animated:YES completion:nil];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}

-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT-kNaviHeight-60) style:(UITableViewStyleGrouped)];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _myTableView.estimatedRowHeight = 90;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.tableFooterView = [UIView new];
        //        _myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
        //            [_myTableView.mj_header beginRefreshing];
        //        }];
        self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self request];//请求数据
            
        }];
        
        [_myTableView registerNib:[UINib nibWithNibName:@"BshopAminTableViewCell" bundle:nil] forCellReuseIdentifier:@"BshopAminTableViewCell"];
        adjustInset(_myTableView);
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

@end
